package com.fingard.dsp.bank.directbank.allinpay02;

import com.fingard.dsp.bank.directConfig.ActSetItem;
import com.fingard.dsp.bank.directbank.bankInterface.IDeContract;
import com.fingard.dsp.bank.reqrespobj.contract.ContractState;
import com.fingard.dsp.bank.reqrespobj.decontract.ReqDeContract;
import com.fingard.dsp.bank.reqrespobj.decontract.RetAtsDeContract;
import com.fingard.dsp.bank.reqrespobj.decontract.detail.RetAtsDeContractDetail;
import com.fingard.text.StringHelper;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

/**
 * 通联商业委托解约/协议解约
 *
 * @author fanghh
 * @date 2018/7/27
 */
public class AllInPay02DeContract extends AllInPay02Base implements IDeContract {
    @Override
    public RetAtsDeContract deContract(ReqDeContract p_req) throws Exception {
        RetAtsDeContract retAts = new RetAtsDeContract(p_req.reqHeader);
        RetAtsDeContractDetail tmpRetDetail = new RetAtsDeContractDetail();
        tmpRetDetail.reqSeqID = p_req.contractDetail.askNumber;

        if ("9108Agrm".equals(p_req.reqHeader.commandCode)) {
            tmpRetDetail = deContractBy9108Agrm(p_req);
        } else if ("9108Auth".equals(p_req.reqHeader.commandCode)) {
            tmpRetDetail = deContractBy9108Auth(p_req);
        } else {
            tmpRetDetail.respInfo = "不支持的指令类型：" + p_req.reqHeader.transType + "-" + p_req.reqHeader.commandCode;
        }

        retAts.writeRetDetail(tmpRetDetail);
        return retAts;
    }

    /**
     * 通联协议解约
     *
     * @param p_req
     * @return
     * @throws Exception
     */
    public RetAtsDeContractDetail deContractBy9108Agrm(ReqDeContract p_req) throws Exception {
        RetAtsDeContractDetail tmpRetDetail = new RetAtsDeContractDetail();

        try {
            ActSetItem actItem = getBankConfig().getActSetItem(p_req.contractDetail.recAct, "contract");
            if (actItem != null) {
                XmlTextWriter xmlWriter = new XmlTextWriter();
                AllInPay02Header header = new AllInPay02Header();
                header.TRX_CODE = "310003";
                header.REQ_SN = p_req.contractDetail.askNumber;

                writeContractCommonHeader(xmlWriter, header, actItem);

                xmlWriter.writeStartElement("FAGRCNL");
                xmlWriter.writeElementString("MERCHANT_ID", actItem.merchantId);
                xmlWriter.writeElementString("ACCOUNT_NO", p_req.contractDetail.payAct);
                xmlWriter.writeElementString("AGRMNO", p_req.contractDetail.authCode);
                xmlWriter.writeEndDocument();

                String[] tmpStrRet = sendContractToBank(xmlWriter.toXmlString(), actItem);

                if (tmpStrRet[0].length() == 0) {
                    StringBuilder tmpSbMsg = new StringBuilder();
                    XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                    if (tmpBkReader != null) {
                        String tmpHeadRespCode = tmpBkReader.getFirstText("/AIPG/INFO/RET_CODE");
                        String tmpHeadRespInfo = tmpBkReader.getFirstText("/AIPG/INFO/ERR_MSG");
                        String tmpRespCode = tmpBkReader.getFirstText("/AIPG/FAGRCNLRET/RET_CODE");
                        String tmpRespInfo = tmpBkReader.getFirstText("/AIPG/FAGRCNLRET/ERR_MSG");

                        tmpRetDetail.respCode = "0";
                        tmpRetDetail.respInfo = tmpHeadRespInfo;

                        if ("0000".equals(tmpHeadRespCode)) {
                            if ("0000".equals(tmpRespCode) || "3047".equals(tmpRespCode)) {
                                tmpRetDetail.deSignState = ContractState.SUCCESS;
                            } else {
                                tmpRetDetail.deSignState = ContractState.FAIL;
                            }
                            if (StringHelper.hasAnyChar(tmpRespInfo)) {
                                tmpRetDetail.respInfo = tmpRespInfo;
                            }
                        } else {
                            tmpRetDetail.respCode = "-1";
                            tmpRetDetail.respInfo = tmpHeadRespInfo;
                        }
                    } else {
                        tmpRetDetail.respCode = "-1";
                        tmpRetDetail.respInfo = tmpSbMsg.toString();
                    }
                } else {
                    tmpRetDetail.respCode = "-1";
                    tmpRetDetail.respInfo = tmpStrRet[1];
                }
            } else {
                tmpRetDetail.respCode = "-1";
                tmpRetDetail.respInfo = "未找到账号:[" + p_req.contractDetail.recAct + "]商户配置信息!";
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            tmpRetDetail.respCode = "-1";
            tmpRetDetail.respInfo = ex.getMessage();
            WriteBankLogLn(ex);
        }

        return tmpRetDetail;
    }

    /**
     * 通联商业委托解约
     *
     * @param p_req
     * @return
     * @throws Exception
     */

    public RetAtsDeContractDetail deContractBy9108Auth(ReqDeContract p_req) throws Exception {
        RetAtsDeContractDetail tmpRetDetail = new RetAtsDeContractDetail();

        try {
            ActSetItem actItem = getBankConfig().getActSetItem(p_req.contractDetail.recAct, "contract");
            if (actItem != null) {
                XmlTextWriter xmlWriter = new XmlTextWriter();
                AllInPay02Header header = new AllInPay02Header();
                header.TRX_CODE = "340003";
                header.REQ_SN = p_req.contractDetail.askNumber;

                writeAgrmCommonHeader(xmlWriter, header, actItem);

                xmlWriter.writeStartElement("AGRCNL");
                xmlWriter.writeElementString("MERCHANT_ID", actItem.merchantId);
                xmlWriter.writeElementString("ACCOUNT_NO", p_req.contractDetail.payAct);
                xmlWriter.writeElementString("AGRMNO", p_req.contractDetail.authCode);
                xmlWriter.writeEndDocument();

                String[] tmpStrRet = sendContractToBank(xmlWriter.toXmlString(), actItem);

                if (tmpStrRet[0].length() == 0) {
                    StringBuilder tmpSbMsg = new StringBuilder();
                    XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                    if (tmpBkReader != null) {
                        String tmpHeadRespCode = tmpBkReader.getFirstText("/AIPG/INFO/RET_CODE");
                        String tmpHeadRespInfo = tmpBkReader.getFirstText("/AIPG/INFO/ERR_MSG");
                        String tmpRespCode = tmpBkReader.getFirstText("/AIPG/AGRRSP/RET_CODE");
                        String tmpRespInfo = tmpBkReader.getFirstText("/AIPG/AGRRSP/ERR_MSG");

                        tmpRetDetail.respCode = "0";
                        tmpRetDetail.respInfo = tmpHeadRespInfo;

                        if ("0000".equals(tmpHeadRespCode)) {
                            if ("0000".equals(tmpRespCode)) {
                                tmpRetDetail.deSignState = ContractState.SUCCESS;
                            } else {
                                tmpRetDetail.deSignState = ContractState.FAIL;
                            }
                            if (StringHelper.hasAnyChar(tmpRespInfo)) {
                                tmpRetDetail.respInfo = tmpRespInfo;
                            }
                        } else if ("1000".equals(tmpHeadRespCode)) {
                            tmpRetDetail.deSignState = ContractState.FAIL;
                            tmpRetDetail.respInfo = "该银行不支持实时解约，请您联系发卡进行解约";
                        } else {
                            tmpRetDetail.respCode = "-1";
                            tmpRetDetail.respInfo = tmpHeadRespInfo;
                        }
                    } else {
                        tmpRetDetail.respCode = "-1";
                        tmpRetDetail.respInfo = tmpSbMsg.toString();
                    }
                } else {
                    tmpRetDetail.respCode = "-1";
                    tmpRetDetail.respInfo = tmpStrRet[1];
                }
            } else {
                tmpRetDetail.respCode = "-1";
                tmpRetDetail.respInfo = "未找到账号:[" + p_req.contractDetail.recAct + "]商户配置信息!";
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            tmpRetDetail.respCode = "-1";
            tmpRetDetail.respInfo = ex.getMessage();
            WriteBankLogLn(ex);
        }

        return tmpRetDetail;
    }

    @Override
    public RetAtsDeContract qryDeContract(ReqDeContract p_reqQry) throws Exception {
        RetAtsDeContract retAts = new RetAtsDeContract(p_reqQry.reqHeader);

        RetAtsDeContractDetail tmpRetDetail = new RetAtsDeContractDetail();
        tmpRetDetail.respCode = "-1";
        if ("9108Auth".equals(p_reqQry.reqHeader.commandCode)) {
            tmpRetDetail.respInfo = "通联渠道不支持商业委托解约查询!";
        } else if ("9108Agrm".equals(p_reqQry.reqHeader.commandCode)) {
            tmpRetDetail.respInfo = "通联渠道不支持协议解约查询!";
        } else {
            tmpRetDetail.respInfo = "不支持的指令类型：" + p_reqQry.reqHeader.transType + "-" + p_reqQry.reqHeader.commandCode;
        }

        retAts.writeRetDetail(tmpRetDetail);
        return retAts;
    }
}
